<template>
  <div class="search-box">
    <i class="icon-search"></i>
    <input type="text" v-model="query" class="box" :placeholder="placeholder">
    <i @click="clear" class="icon-dismiss" v-show="query"></i>
  </div>
</template>

<script type="text/ecmascript-6">
  export default {
    props: {
      placeholder: {
        type: String,
        default: '请输入歌曲、歌手'
      }
    },
    data () {
      return {
        query: ''
      }
    },
    methods: {
      // 清除输入框的内容
      clear () {
        this.query = ''
      },
      // t提供给外部的接口，
      // 当点击推荐词就会添加到搜索框
      setQuery (query) {
        this.query = query
      }
    },
    created () {
      // 把query变量暴露给外面
      this.$watch('query', (newVal) => {
        this.$emit('query', newVal)
      })
    }
  }
</script>

<style scoped lang="stylus">
  @import "~common/stylus/variable.styl"
  .search-box
    display: flex
    align-items: center
    box-sizing: border-box
    width: 100%
    height: 40px
    padding: 0 6px
    border-radius: 6px
    background: $color-highlight-background

    .icon-search
      font-size: 24px
      color: $color-background

    .box
      flex: 1
      margin: 0 5px
      line-height: 18px
      color: $color-text
      font-size: $font-size-medium
      background: $color-highlight-background

    .icon-dismiss
      font-size: 16px
      color: $color-background
</style>
